<?php

App::uses('AdminController', 'Controller');
App::uses('MyCache', 'Lib');

class AdminProductsController extends AdminController {
    
    public $uses = array('Product', 'Group');
    
    public function index($groupId=null) {
        $allGroups = $this->Group->find('list');
    
        $products  = array();
        $groupName = '';
        $groupHome = 0;
        if ($groupId>0) {
            $group    = $this->Group->findById($groupId);
            $products = $this->paginate('Product', array(
                'Product.group_id' => $groupId
            ));
            $groupName = $group['Group']['name'];
            $groupHome = (int)$group['Group']['home'];
        }
    
        $this->set(compact('groupId', 'allGroups', 'products', 'groupName', 'groupHome'));
    }
    
    protected function _setCommonData($groupId) {
        $this->set('categories', $this->Group->find('list'));
        $this->set('groupId', $groupId);
    }
    
    public function create($groupId) {
        $this->_setCommonData($groupId);
        
        $data = $this->Product->create(array(
            'group_id' => $groupId
        ));
        
        $this->set(compact('data'));
        $this->render('form');
    }
    
    public function edit($id) {
        
        $data    = $this->Product->findById($id);
        $groupId = $data['Product']['group_id'];
        
        $this->_setCommonData($groupId);
        $this->set(compact('data'));
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        
        if (! empty($data)) {
            $data['Product']['key'] = urlize($data['Product']['name']);
            
            $this->Product->create($data);
            if (! $this->Product->validates()) {
                $this->_setCommonData($data['Product']['group_id']);
                $this->set('data', $data);
                $this->setFlash('Uneti podaci nisu validni.');
                return $this->render('form');
            }
            
            $toUpload = array();
            foreach ($data['Product']['NewProductImage'] as $img) {
                if (! $img['error']) {
                    $toUpload[] = $img;
                    
                    if (empty($data['ProductImage'])) {
                        $data['ProductImage'] = array();
                    }
                    $data['ProductImage'][] = array(
                        'src' => $img['name']
                    );
                }
            }
            
            $this->Product->deleteRelatedData();
            if (! $this->Product->saveAll($data)) {
                $this->_setCommonData($data['Product']['group_id']);
                $this->set('data', $data);
                $this->setFlash('Greška pri čuvanju.');
                return $this->render('form');
            }
            
            $imageBase = WWW_ROOT . 'img' . DS . 'products' . DS . $this->Product->id;
            
            // remove old images
            $except = array();
            if (! empty($data['ProductImage'])) {
                $except = Set::extract('{n}.src', $data['ProductImage']);
            }
            delete_files($imageBase, $except);
            
            // upload new images
            if (! empty($toUpload)) {
                foreach ($toUpload as $upload) {
                    $destination = $imageBase . DS . $upload['name'];
                    $cacheDir  = 'products' . DS . $this->Product->id;
                    MyCache::emptyDir($cacheDir);
                    upload_file($upload['tmp_name'], $destination);
                }
            }
            
            $this->setFlash('Proizvod je sačuvan.', 'ok');
            $this->redirect('/admin_products/edit/' . $this->Product->id);
        }
        
        $this->redirect('index');
    }
    
    public function delete($id) {
        $product = $this->Product->findById($id);
        $groupId = $product['Group']['id'];
        if ($this->Product->delete($id)) {
            $this->setFlash('Proizvod je obrisan.', 'ok');
        } else {
            $this->setFlash('Greška pri brisanju proizvoda.');
        }
        $this->redirect("index/{$groupId}");
    }
    
}